package com.xuexiang.xuidemo.slice.popupwindow;

import com.xuexiang.xui_lib.component.toast.XToast;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.provider.XToastItemProvider;
import com.xuexiang.xuidemo.slice.BaseAbilitySlice;
import com.xuexiang.xuidemo.util.XToastUtils;
import com.xuexiang.xuidemo.view.TitleBarComponent;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;

import java.util.ArrayList;
import java.util.List;

public class XToastAbilitySlice extends BaseAbilitySlice {
    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_xtoast;
    }

    @Override
    protected int getTitleBarId() {
        return ResourceTable.Id_title_bar;
    }

    @Override
    protected void initComponents() {
        super.initComponents();
        XToast.Config.get()
            //位置设置为居中
            .setGravity(LayoutAlignment.CENTER);

        initView();
    }

    private void initView() {
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        List<String> list = getData();
        XToastItemProvider sampleItemProvider = new XToastItemProvider(list, this);
        listContainer.setItemProvider(sampleItemProvider);
        listContainer.setBoundaryColor(new Color(Color.getIntColor("#C4C8D0")));
        listContainer.setBoundarySwitch(true);
        listContainer.setItemClickedListener((listContainer1, component, position, l) -> onListItemClick(position));
    }

    private ArrayList<String> getData() {
        ArrayList<String> lists = new ArrayList<>();
        lists.add("ERROR_TOAST");
        lists.add("SUCCESS_TOAST");
        lists.add("INFO_TOAST");
        lists.add("WARNING_TOAST");
        lists.add("NORMAL_TOAST");
        return lists;
    }

    private void onListItemClick(int position) {
        switch (position) {
            case 0:
                XToastUtils.error(ResourceTable.String_error_message);
                break;
            case 1:
                XToastUtils.success(ResourceTable.String_success_message);
                break;
            case 2:
                XToastUtils.info(ResourceTable.String_info_message);
                break;
            case 3:
                XToastUtils.warning(ResourceTable.String_warning_message);
                break;
            case 4:
                XToastUtils.toast(ResourceTable.String_normal_message_without_icon);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        XToast.Config.get()
            .resetGravity();
    }
}
